package com.example.zsf.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.zsf.R;
import com.example.zsf.activity.mobile.MobileDetailActivity;
import com.example.zsf.adapter.CollectRecordListAdapter;
import com.example.zsf.base.Presenter;
import com.example.zsf.base.Result;
import com.example.zsf.base.ZSFBaseActivity;
import com.example.zsf.model.CollectRecordModel;
import com.example.zsf.model.MobileModel;
import com.example.zsf.presenter.MinePresenter;
import com.example.zsf.utils.DebouncedOnItemClickListener;
import com.example.zsf.view.CollectedInfoDialog;
import com.example.zsf.view.EnterInfomationDialog;
import com.example.zsf.view.NavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CollectRecordActivity extends ZSFBaseActivity {

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private int curPage = 1;
    private CollectRecordListAdapter collectRecordListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect_record);
        ButterKnife.bind(this);
        initUI();
        requestCollectRecord(curPage);
    }

    private void initUI() {
        Window window = getWindow();
        View decorView = window.getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }


        navigationBar.setTitleText("领取记录");
        navigationBar.setLeftWhite();
        navigationBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        collectRecordListAdapter = new CollectRecordListAdapter(this, R.layout.item_collect_record, null);
        recyclerView.setAdapter(collectRecordListAdapter);
        collectRecordListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                //待填写的回调
                curPage = 1;
                requestCollectRecord(curPage);
            }
        });
        collectRecordListAdapter.setOnItemClickListener(new DebouncedOnItemClickListener(400) {
            @Override
            public void onDebouncedItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                CollectRecordModel.CollectRecordListModel collectRecordListModel = (CollectRecordModel.CollectRecordListModel) adapter.getItem(position);
                int status = Integer.parseInt(collectRecordListModel.getRcv_status());
                if (status == 4) {
                    //待填写
                    EnterInfomationDialog enterInfomationDialog =
                            new EnterInfomationDialog(CollectRecordActivity.this,
                                    null, collectRecordListModel, new EnterInfomationDialog.EnterInfoListener() {
                                @Override
                                public void confirmCallBack() {
                                    curPage = 1;
                                    requestCollectRecord(curPage);
                                }
                            });
                    enterInfomationDialog.show();
                } else {
                    CollectedInfoDialog collectedInfoDialog = new CollectedInfoDialog(CollectRecordActivity.this,null,collectRecordListModel);
                    collectedInfoDialog.show();
                }
            }
        });


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                curPage = 1;
                requestCollectRecord(curPage);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                curPage++;
                requestCollectRecord(curPage);
            }
        });

    }


    private void requestCollectRecord(int page){
        disposed(MinePresenter.getCollectRecord(page, new Presenter.PresenterListener<Result<CollectRecordModel>>() {
            @Override
            public void onSuccess(Result<CollectRecordModel> object) {
                if (object.code.equals("1")) {
                    if (curPage == 1) {
                        collectRecordListAdapter.setList(object.data.getRcv());
                        collectRecordListAdapter.notifyDataSetChanged();
                        refreshLayout.finishRefresh();
                        return;
                    }
                    if (object.data.getRcv().size() > 0) {
                        collectRecordListAdapter.addData(object.data.getRcv());
                        collectRecordListAdapter.notifyDataSetChanged();
                        refreshLayout.finishLoadMore();
                    } else {
                        refreshLayout.finishLoadMore();
                        refreshLayout.setNoMoreData(true);
                        curPage--;
                    }
                } else {
                    ToastUtils.showShort(object.msg);
                    refreshLayout.finishRefresh(false);
                    refreshLayout.finishLoadMore(false);
                    curPage--;
                }
            }

            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
                refreshLayout.finishRefresh(false);
                refreshLayout.finishLoadMore(false);
                curPage--;
            }
        }));
    }
}